#!/bin/sh
# /**
# * Copyright (c) 2018, God's harbor
# * All rights reserved.
# * @file    mkclass.sh
# * @brief   生成C++的类文件
# * @author  Jason Zhou,yzfcer@163.com
# * @version 1.0
# * @date    2021-09-15
# */

function usage()
{
    echo -e "mkclass.sh uasge:"
    echo -e "mkclass.sh <directory> <file_name> "
}

function fill_header_file()
{
    local full_file_path=$1
    local class_name=$2
    local class_str="C"${class_name^}
    echo -e "\#ifndef CLASS_"${class_name^^}"_H__" >> $full_file_path
    echo -e "\#define CLASS_"${class_name^^}"_H__" >> $full_file_path
    echo -e "#include \"streamaxtype.h\"" >> $full_file_path
    echo -e "\n" >> $full_file_path

    # 添加变量结构体
    echo -e "typedef struct " >> $full_file_path
    echo -e "{" >> $full_file_path >> $full_file_path
    echo -e "    xint32_t flag; // test variable" >> $full_file_path
    echo -e "}C"${class_name^}"Mbr_t;" >> $full_file_path
    echo -e "\n" >> $full_file_path

    # 添加类成员声明
    echo -e "class C"${class_name^} >> $full_file_path
    echo -e "{" >> $full_file_path
    echo -e "    public:" >> $full_file_path
    echo -e "        C"${class_name^}"();" >> $full_file_path
    echo -e "        ~C"${class_name^}"();" >> $full_file_path
    echo -e "\n" >> $full_file_path
    echo -e "    private:" >> $full_file_path
    echo -e "        C"${class_name^}"Mbr_t m;" >> $full_file_path
    echo -e "};" >> $full_file_path
    echo -e "\n" >> $full_file_path
    echo -e "#endif // #ifndef CLASS_"${class_name^^}"_H__" >> $full_file_path
    echo -e "\n" >> $full_file_path
}


function fill_source_file()
{
    local full_file_path=$1
    local class_name=$2
    local class_str="C"${class_name^}
    echo -e "#include \""${class_name}".h\"" >> $full_file_path
    echo -e "#include \"string.h\"" >> $full_file_path
    echo -e "\n" >> $full_file_path

    # 添加构造函数
    echo -e ${class_str}"::"${class_str}"()" >> $full_file_path
    echo -e "{" >> $full_file_path
    echo -e "    memset(&m,0,sizeof(m));" >> $full_file_path
    echo -e "}" >> $full_file_path

    # 添加析构函数
    echo -e ${class_str}"::~"${class_str}"()" >> $full_file_path
    echo -e "{" >> $full_file_path
    echo -e "}" >> $full_file_path

    echo -e "\n" >> $full_file_path

}

function check_args()
{
    if [ $# -lt 2 ]
    then
        echo -e "miss argument"
        usage
        exit
    fi
}

function delete_old_files()
{
    local dir_name=$1
    local file_name=$2
    if [ -f ${dir_name}/${file_name}.h ]
    then
        rm -rf ${dir_name}/${file_name}.h
    fi
    if [ -f ${dir_name}/${file_name}.cpp ]
    then
        rm -rf ${dir_name}/${file_name}.cpp
    fi
    rm -rf ${dir_name}/${file_name}.h ${dir_name}/${file_name}.cpp
}

function create_files()
{
    local dir_name=$1
    local file_name=$2
    touch ${dir_name}/${file_name}.h
    touch ${dir_name}/${file_name}.cpp
    fill_header_file ${dir_name}/${file_name}.h ${file_name}
    fill_source_file ${dir_name}/${file_name}.cpp ${file_name}
}

function main()
{
    check_args $@
    delete_old_files $@
    create_files $@
}
main $@
